package com.wang.common.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wang.common.pojo.InfoEnum;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.DateFormatUtil;
import com.wang.common.util.EncryptUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.common.util.RegexUtil;

/**
 * 存储的是所有Servlet层的公共代码 ~工具类 ~所有公共的代码也放过来
 * 
 * @author wangshMac
 */
public class BaseServlet extends HttpServlet
{
	/**
	 * 前缀
	 */
	private String prefix = "/WEB-INF/jsp/";
	/**
	 * 后缀
	 */
	private String subbfix = ".jsp";
	
	/**
	 * 提示信息
	 */
	protected String info = "" ; 
	
	/* jsp文件路径 */
	protected String path;
	protected String updateFlag ; 
	/* 加密算法工具类 */
	protected EncryptUtil encryptUtil = new EncryptUtil();
	/* 引入工具类 */
	protected DateFormatUtil dateFormatUtil = new DateFormatUtil();
	protected RegexUtil regexUtil = new RegexUtil();
	protected EncryptUtil enuEncryptUtil = new EncryptUtil();
	
	
	/**
	 * 公共的一些操作
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	protected void commonOper(HttpServletRequest request)
	{
		/* 存储一些公共的变量 */
		request.setAttribute("websiteUrl", ConstatFinalUtil.SYSTEM_JSON.get("website.url"));
		request.setAttribute("websiteFileUrl", ConstatFinalUtil.SYSTEM_JSON.get("website.fileUrl"));
		request.setAttribute("websiteHeadUrl", ConstatFinalUtil.SYSTEM_JSON.get("website.head.url"));
	}
	
	public void init(ServletConfig config) throws ServletException
	{
		ServletContext servletContext = config.getServletContext();
		/* 前缀 */
		String prefix = servletContext.getInitParameter("prefix");
		if(prefix != null && !"".equalsIgnoreCase(prefix))
		{
			this.prefix = prefix ; 
		}
		/* 后缀 */
		String subbfix = servletContext.getInitParameter("subbfix");
		if(subbfix != null && !"".equalsIgnoreCase(subbfix))
		{
			this.subbfix = subbfix ; 
		}
	}
	
	/**
	 * 服务器端跳转
	 * @throws IOException 
	 * @throws ServletException 
	 */
	protected void serverRedirect(HttpServletRequest request,HttpServletResponse response,String path) throws ServletException, IOException
	{
		/* 拼装路径 */
		path = this.prefix + path + this.subbfix ; 
		/* 服务器端跳转,又叫转发器 */
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 客户器端跳转
	 * @throws IOException 
	 * @throws ServletException 
	 */
	protected void clientRedirect(HttpServletRequest request,HttpServletResponse response,String path) throws ServletException, IOException
	{
		/* 拼装路径 */
		path = request.getContextPath() + path ; 
		/* 客户端端跳转,又叫重定向 */
		response.sendRedirect(path);
	}
	
	/**
	 * 白页面
	 * @throws IOException 
	 */
	protected void blank(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		/* 错误信息 */
		String info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_ERROR_BLANK.getCode() + "") ;
		this.printStr(request, response, info);
	}
	
	/**
	 * 输出字符串给浏览器
	 * @throws IOException 
	 */
	protected void printStr(HttpServletRequest request,HttpServletResponse response,
			String info) throws IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		/* 发送信息 */
		out.println(info);
		out.flush();
		out.close();
	}
	
	/**
	 * 生成一个查询列表公共的搜索条件
	 * @param request
	 * @return
	 */
	protected  Map<String, Object> proccedSearch(HttpServletRequest request)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		String keyword = request.getParameter("keyword");
		
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		/* 将查询条件存储到request中 */
		request.setAttribute("keyword", keyword);		
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		/* 关键字 */
		if(keyword == null)
		{
			keyword = "" ; 
		}
		
		/* 状态 */
		if(status == null)
		{
			status = "" ; 
		}
		
		/* 查询的起始时间和结束时间 */
		Date stDate = null ; 
		Date edDate = null ;
		if(st != null && !"".equalsIgnoreCase(st) && ed != null && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.strDateTime(st);
			edDate = this.dateFormatUtil.strDateTime(ed);
		}
		condMap.put("keyword", keyword);		
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		return condMap;
	}
	
	/**
	 * 生成分页对象
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	protected PageInfoUtil proccedPageInfo(HttpServletRequest request)
	{
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		/* 分页 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		try
		{
			/* 将字符串转换成整数,有风险,
			 * 字符串为a,转换不成不整数
			 *  */
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfoUtil;
	}
	
	/**
	 * 从session中获取用户或者管理员信息
	 * @return
	 */
	protected Object findObjfromSession(HttpServletRequest request , String type)
	{
		HttpSession session = request.getSession();
		if("users".equalsIgnoreCase(type))
		{
			return session.getAttribute("users");
		}else if("admins".equalsIgnoreCase(type))
		{
			return session.getAttribute("admins");
		}
		return null ; 
	}
	
	/**
	 * 返回json字符串
	 * @param request
	 * @param info
	 * @return
	 * @throws IOException 
	 */
	public void printOut(String returnStr,HttpServletResponse response)
	{
		try
		{
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(returnStr);
			out.flush();
			out.close();
		} catch (IOException e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("为客户端返回信息出错了;" , e);
		}
	}
}
